$(function () {
    let isUsernameReady = false,
        isPasswordReady = false,
        isMobileReady = false,
        isSmsCodeReady = false;
    //点击刷新图片验证码
    let $img=$('.form-contain .form-item .captcha-graph-img img');
        $img.click(function () {
           $img.attr('src','/virefy/image_code/?rand=' + Math.random());
        });



    //fncheckusername start//
    let $username=$('#username');
    $username.blur(fnCheckUsernam);
    function fnCheckUsernam() {
        isUsernameReady = false;
        let $sUsername = $username.val();
        if ($sUsername === '') {
            message.showError("用户名不能为空");
            return
        }
        if (!(/^\w{5,20}$/).test($sUsername)) {
            message.showError("请输入5-20个字符的用户名");
            return
        }
        $.ajax({
            url: '/virefy' +'/username/' + $sUsername + '/',
            type: 'GET',
            dataType: 'json',
            success: function (data) {
                if (data.data.count !== 0) {
                    message.showError(data.data.username + "用户已经注册");
                } else {
                    message.showInfo(data.data.username + "用户可以使用");
                    isUsernameReady = true;
                }
            }
        })
    }
    //fncheckusername end//



    //fncheckpassword start//
     let $passwordRepeat = $('input[name="password_repeat"]');
    $passwordRepeat.blur(fnCheckPassword);
    function fnCheckPassword() {
        isPasswordReady = false;
        let password = $('input[name="password"]').val();
        let passwordRepeat = $passwordRepeat.val();
        if (password === '' || passwordRepeat === '') {
            message.showError('密码不能为空');
            return
        }
        if (password !== passwordRepeat) {
            message.showError('两次密码输入不一致');
            return
        }
        if (password === passwordRepeat) {
            isPasswordReady = true
        }
    }
    //fncheckpassword end//




    //fncheckmobile start//
    let $telephone=$('input[name="telephone"]');
    $telephone.blur(fnCheckMobile);
    function fnCheckMobile() {
        isMobileReady = false;
        let $dtelephone = $telephone.val();
        if($dtelephone===''){
            message.showError("手机号不能为空");
            return
        }
        if(!(/^1[3-9]\d{9}$/).test($dtelephone)){
            message.showError("手机号格式不正确");
            return
        }
        $.ajax({
        url:'/virefy' +'/mobile/' + $dtelephone + '/',
        type:'GET',
        dataType:'json',
        success:function (data) {
            if(data.data.count!==0){
                message.showError(data.data.mobile+"手机号已经注册");
            }else{
                message.showInfo(data.data.mobile+"手机号可以使用");
                isMobileReady = true;
            }
        },
            error: function (xhr, msg) {
                message.showError('服务器超时，请重试！')
            }
        })
    }

    // //check_image_code//
    // let $captcha_graph=$('#captcha_graph');
    // $captcha_graph.blur(fnCheckCode);
    // function fnCheckCode() {
    //     let $Wcaptcha_graph=$captcha_graph.val();
    //     if($Wcaptcha_graph===''){
    //         message.showError("验证码不能为空");
    //     }
    // }
  // 5.发送手机验证码
    let $smsButton = $('.sms-captcha');
    $smsButton.click(function () {
        let sCaptcha = $('input[name="captcha_graph"]').val();
        if(sCaptcha === ''){
            message.showError('请输入验证码');
            return
        }
        if(!isMobileReady){
            fnCheckMobile();
            return
        }
        $.ajax({
            url: '/virefy' + '/sms_code/',
            type: 'POST',
            data: {
                telephone: $telephone.val(),
                sms_captcha: sCaptcha
            },
            dataType: 'json',
            success: function (data) {
                if(data.errno !== '0'){
                    message.showError(data.errmsg)
                }else {
                    message.showSuccess('发送验证码成功');
                    $smsButton.attr('disabled',true);
                    //设置计时器
                    var num = 60;
                    let t = setInterval(function () {
                        $smsButton.html('倒计时'+ num +'秒');
                        if(num===1){
                            clearInterval(t);
                            $smsButton.removeAttr('disabled');
                            $smsButton.html('获取短信验证码')
                        }num --
                    },1000)
                }
            },
            error: function (xhr, msg) {
                message.showError('服务器超时，请重试！')
            }
        });

    });
    let $btn=$('.register-btn');
    $btn.click(function (e) {
         e.preventDefault();
        if(!isUsernameReady){
            fnCheckUsernam();
            // console.log("用户名OK");
            return
        }
        else if(!isPasswordReady){
            fnCheckPassword();
            // console.log("密码OK");
            return
        }
        if(!isMobileReady){
            fnCheckMobile();
            // console.log("手机号OK");
            return
        }

        $.ajax({
            url:'/user/register/',
            type:'POST',
            dataType: 'json',
            traditional:true,
            async:true,
            data:{
                username:$username.val(),
                password:$password = $('input[name="password"]').val(),
                password_repeat:$passwordRepeat.val(),
                telephone:$telephone.val(),
                sms_captcha:$sms_captcha=$('input[name="sms_captcha"]').val(),
            },
            success:function (data1) {
                if(data1.errno ==='0'){
                     message.showSuccess("恭喜你注册成功");
                         console.log("进入跳转");
                         setTimeout(function () {
                             window.location.href= '/user/login/'
                         },3000)

                }
                else{
                    console.log("进入跳转1");
                   message.showError(data1.errmsg);
                    }
            },
            error: function (xhr, msg) {
                message.showError('服务器超时，请重试！');
            }

        })

    });


});